package be.kuleuven.cs.mop.gui.components;


import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.BoxLayout;
import javax.swing.JPanel;



public class DateInput extends Component {
	private static final long serialVersionUID = 1L;
	
	private final NumericComboBox	day		= new NumericComboBox(1, 31);
	private final Spinner			hours	= new Spinner(24);
	private final Spinner			minutes	= new Spinner(60);
	private final NumericComboBox	month	= new NumericComboBox(1, 12);
	private final NumericComboBox	year	=
		new NumericComboBox(1970, (new GregorianCalendar().get(Calendar.YEAR) - 1870));
	
	
	public DateInput() {
		super();
		final GridBagConstraints gbc = getGBC();
		
		gbc.gridx	= 0;
		gbc.gridy	= 0;
		gbc.insets	= new Insets(3, 3, 0, 5);
		add(new Label("Date:"), gbc);
		
		gbc.gridx	= 1;
		gbc.insets	= new Insets(3, 5, 0, 3);
		final JPanel date = new JPanel();
		date.setLayout(new BoxLayout(date, BoxLayout.LINE_AXIS));
		date.add(day);
		date.add(month);
		date.add(year);
		add(date, gbc);
		
		gbc.gridx	= 0;
		gbc.gridy	= 1;
		gbc.insets	= new Insets(0, 3, 3, 5);
		add(new Label("Time:"), gbc);
		
		gbc.gridx	= 1;
		gbc.insets	= new Insets(0, 5, 3, 3);
		final JPanel time = new JPanel();
		time.add(hours);
		time.add(minutes);
		add(time, gbc);
		
		set(new GregorianCalendar());
	}
	
	
	
	public Calendar read() {
		return new GregorianCalendar(year.read(), (month.read() - 1), day.read(),
				hours.read(), minutes.read());
	};
	
	public void set(final Calendar date) {
		day.setSelectedIndex(date.get(Calendar.DAY_OF_MONTH) - 1);
		hours.setValue(new Integer(date.get(Calendar.HOUR_OF_DAY)));
		minutes.setValue(new Integer(date.get(Calendar.MINUTE)));
		month.setSelectedIndex(date.get(Calendar.MONTH));
		year.setSelectedIndex(date.get(Calendar.YEAR) - 1970);
	}
	
}